package com.chencs.biz.model.controller;

import com.alibaba.fastjson.JSON;
import com.chencs.biz.model.service.ModelService;
import com.chencs.vo.biz.model.ModeBlockModel;
import com.chencs.vo.biz.model.ModeModel;
import com.chencs.vo.biz.product.FavoritesModel;
import framework.model.ResponseOptUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by fangzehua on 2017/7/8.
 */
@RequestMapping("/model")
@RestController
public class ModeController {

    @Autowired
    private ModelService modelService;
    @RequestMapping(value = "/doModelPage", method = RequestMethod.GET)
    public ModelAndView doModelPage() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("admin/biz/model/model_list");
        return modelAndView;
    }


    @RequestMapping(value = "/modelList", method = RequestMethod.GET)
    public String categoryList(){
        String retStr = "";
        try {
            retStr = modelService.modelList();
        } catch (Exception e) {
            retStr = ResponseOptUtil.commonMsgError( ResponseOptUtil.RET_FAIL_MSG);
            e.printStackTrace();
        }
        return retStr;
    }


    @RequestMapping(value = "/doEditor/{id}", method = RequestMethod.GET)
    public ModelAndView doModelPage(@PathVariable("id") String id , Model model) {
        ModeModel modeModel = null;
        try{
            modeModel = JSON.parseObject(modelService.findModelById(id),ModeModel.class);
        }catch (Exception e){
            e.printStackTrace();
        }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("admin/biz/model/model_editor");
        model.addAttribute("modeModel",modeModel);
        return modelAndView;
    }


    @RequestMapping(value = "/setParameter/{id}", method = RequestMethod.GET)
    public ModelAndView setParameter(@PathVariable("id") String id , Model model) {
        ModeBlockModel modeModel = null;
        try{
            modeModel = JSON.parseObject(modelService.findModeBlockModelByMid(id),ModeBlockModel.class);
        }catch (Exception e){
            e.printStackTrace();
        }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("admin/biz/model/model_parameter");
        model.addAttribute("modeModel",modeModel);
        return modelAndView;
    }

    @RequestMapping(value = "/updateParameter", method = RequestMethod.POST)
    public String updateParameter(@RequestBody  ModeBlockModel model) {
        String  retStr = null;
        try {
            retStr =  modelService.updateParameter(model);
        }catch (Exception e){
            retStr =  ResponseOptUtil.commonMsgError("参数设置错误");
            e.printStackTrace();
        }
        return retStr;
    }



    @RequestMapping(value = "/delModel", method = RequestMethod.POST)
    public String delModel(@RequestBody String id) {
        String  retStr = null;
        try {
            retStr =  modelService.delModel(id);
        }catch (Exception e){
            retStr =  ResponseOptUtil.commonMsgError("修改模块失败");
            e.printStackTrace();
        }
        return retStr;
    }

    @RequestMapping(value = "/doAddModelPage", method = RequestMethod.GET)
    public ModelAndView doAddModelPage() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("admin/biz/model/model_add");
        return modelAndView;
    }

    @RequestMapping(value = "/updateModel", method = RequestMethod.POST)
    public String updateModel(@RequestBody ModeModel modeModel) {
        String  retStr = null;
         try {
             retStr =  modelService.updateModel(modeModel);
         }catch (Exception e){
             retStr =  ResponseOptUtil.commonMsgError("修改模块失败");
             e.printStackTrace();
         }
        return retStr;
    }

    @RequestMapping(value = "/insertModel", method = RequestMethod.POST)
    public String insertModel(@RequestBody ModeModel modeModel) {
        String  retStr = null;
        try {
            retStr =  modelService.insertModel(modeModel);
        }catch (Exception e){
            retStr =  ResponseOptUtil.commonMsgError("模块添加成功");
            e.printStackTrace();
        }
        return retStr;
    }


}
